// UrlMouseSensitive.cpp: implementation of the CUrlMouseSensitive class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ansiterminal.h"
#include "UrlMouseSensitive.h"

#include "strFuncs.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CUrlMouseSensitive::CUrlMouseSensitive()
{
	m_urlRect.left = -1;
}

CUrlMouseSensitive::~CUrlMouseSensitive()
{

}

BOOL CUrlMouseSensitive::CheckSensitive(CAnsiScreen *pScreen, CPoint ptCell, UINT nFlags)
{
	m_pScreen = pScreen;
	char** chars = pScreen->GetChars();
	char* line = chars[ptCell.y];
	
	int x1, x2;
	m_url = strFuncs::CheckUrl(line, ptCell.x, &x1, &x2);
	if(!m_url.IsEmpty())
	{
		CRect urlRect(x1, ptCell.y, x2, ptCell.y);
		if(urlRect != m_urlRect)
		{
			if(m_urlRect.left != -1)
				setScreenEffects(m_urlRect, EffectNone);

			m_urlRect = urlRect;
			setScreenEffects(m_urlRect, EffectHighlight);
		}

		pScreen->SetCursor(LoadCursor(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDC_URL)));
		return TRUE;
	}
	return FALSE;
}

BOOL CUrlMouseSensitive::Click(CAnsiScreen *pScreen, CPoint ptCell, UINT nFlags)
{
	AfxMessageBox(m_url);
	return TRUE;
}

void CUrlMouseSensitive::Activate()
{

}

void CUrlMouseSensitive::Deactivate()
{
	setScreenEffects(m_urlRect, EffectNone);
	m_urlRect.left = -1;
}

void CUrlMouseSensitive::setScreenEffects(RECT* pRect, BYTE effect)
{
	for(int y = pRect->top; y <= pRect->bottom; y++)
	{
		for(int x = pRect->left; x <= pRect->right; x++)
		{
			m_pScreen->SetEffect(x, y, effect);
		}
	}
}